package top.xana.acg.base

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.CallSuper
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.navigation.NavDestinationDsl
import androidx.navigation.NavDirections
import androidx.navigation.findNavController
import com.skydoves.bindables.BindingFragment
import top.xana.acg.anime.utils.ToastUtils

/**
 * @author xana
 * @date 2022/8/13 0013 21:12
 * @email cv4096@qq.com
 **/

abstract class BaseFragment<T : ViewDataBinding>(@LayoutRes id: Int) : BindingFragment<T>(id){
    companion object {
        const val TAG = "BaseFragment"

        private val mCalledField = Fragment::class.java.getDeclaredField("mCalled")
    }

    private var mBinding: T? = null

    private var firstInit: Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initArgs(savedInstanceState)
    }

    @CallSuper
    final override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        if (mBinding == null) {
            super.onCreateView(inflater, container, savedInstanceState)
            mBinding = binding
        }
        return checkNotNull(mBinding?.root)
    }

    final override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (firstInit)
            return
        initView()
        initData()
        firstInit = true
    }

    @SuppressLint("MissingSuperCall")
    override fun onDestroyView() {
        mCalledField.isAccessible = true
        mCalledField.set(this, true)
    }

    @CallSuper
    override fun onDestroy() {
        super.onDestroyView()
        super.onDestroy()
        mBinding = null
        firstInit = false
    }

    protected open fun initView() = Unit

    protected open fun initData() = Unit

    protected open fun initArgs(bundle: Bundle?) = Unit

    protected fun navTo(dir: NavDirections) = view?.findNavController()?.navigate(dir)

    protected fun navTo(@IdRes pageId: Int) = view?.findNavController()?.navigate(pageId)

    protected fun navTo(intent: Intent) = context?.let {
        it.startActivity(intent)
    }

    protected fun toast(@StringRes id: Int, duration: Int = Toast.LENGTH_SHORT) =
        ToastUtils.showShort(id)

    protected fun toast(msg: String, duration: Int = Toast.LENGTH_SHORT) = ToastUtils.showShort(msg)

    inline fun <T, R> take(receiver: T, block: T.() -> R): Unit {
        receiver.block()
    }

    protected fun finish(@NavDestinationDsl destId: Int) = view?.findNavController()?.popBackStack(destId, true)

}